<template>
  <div class="swiper" @mouseover="stopPlay" @mouseleave="autoPlay" >
    <img v-for="(swiper, index) in data" :src="swiper.url"
         v-show="index === num" @click="$emit('click', swiper)"/>
    <div class="dot">
      <span v-for="(swiper, index) in data"
            :class="{active: index === num}"
            @click="num = index"></span>
    </div>
    <a class="left" @click="prevImg"></a>
    <a class="right" @click="nextImg"></a>
  </div>
</template>

<script>
export default {
  props: {
    data: {
      type: Array,
      required: true
    },
    duration: {
      type: Number,
      required: false,
      default: 2000,
    }
  },
  data(){
    return {
      num: 0 ,
      playing: false
    }
  },
  created(){
     this.autoPlay();
  },
  methods: {
    stopPlay(){
       clearInterval(this.playing);
    },
    autoPlay() {
      this.playing = setInterval(this.nextImg, this.duration) ;
    },
    prevImg() {
      if (this.num === 0) {
          this.num = this.data.length - 1 ;
      }else{
        this.num -- ;
      }
    },
    nextImg() {
       if (this.num === this.data.length - 1) {
          this.num = 0 ;
       }else{
          this.num ++ ;
       }
    }
  }
}
</script>

<style scoped>
.swiper {
  position: relative;
  width: 100%;
  height: 300px;
  z-index: 99;
  top: 48px;
  margin: 0 auto;
}

.swiper img {
  width: 100%;
  height: 100%;
}

.swiper .dot {
  position: absolute;
  bottom: 0;
  width: 100%;
  height: 50px;
  line-height: 54px;
  background-color: rgba(0, 0, 0, .5);
  text-align: center;
  font-size: 0;
  z-index: 9999999;
  cursor: pointer;
}

.swiper .dot span {
  display: inline-block;
  width: 40px;
  height: 4px;
  background-color: #dfdfdf;
  margin: 0 5px;
  border-radius: 5px;
}

.swiper .dot span.active {
  background-color: #fff;
}

.swiper a.left , .swiper a.right {
  position: absolute;
  width: 48px;
  height:48px;
  top: 126px;
  display: none;
}

.swiper:hover a.left , .swiper:hover a.right {
  display:block;
}

.swiper a.left {
  background-image: url(../assets/images/left-swiper-1.png);
}

.swiper a.left:hover {
  background-image: url(../assets/images/left-swiper-2.png);
}

.swiper a.right {
  background-image: url(../assets/images/right-swiper-1.png);
  right: 0;
}

.swiper a.right:hover {
  background-image: url(../assets/images/right-swiper-2.png);
  right: 0;
}
</style>